package com.siyao.redvip.components.imagepicker.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

import java.io.Serializable;
import java.util.List;

import com.siyao.redvip.R;
import com.siyao.redvip.components.base.BaseActivity;
import com.siyao.redvip.components.imagepicker.adapter.ImageBucketAdapter;
import com.siyao.redvip.components.imagepicker.bean.ImageBucket;
import com.siyao.redvip.components.imagepicker.utils.AlbumHelper;
import com.siyao.redvip.components.imagepicker.utils.Bimp;
import com.siyao.redvip.utils.Constants;
import com.siyao.redvip.utils.Track;

public class ImageAlbumActivity extends BaseActivity {
	// ArrayList<Entity> dataList;//用来装载数据源的列表
	List<ImageBucket> dataList;
	GridView gridView;
	ImageBucketAdapter adapter;// 自定义的适配器
	AlbumHelper helper;
	public static final String EXTRA_IMAGE_LIST = "imagelist";
	public static Bitmap bimap;
	private int maxSize = Constants.DEFAULT_IMAGE_MAX_SIZE;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Track.i("ImageAlbumActivity");
	}

	@Override
	protected void beforeInit() {

	}

	@Override
	protected void setContentView() {
		View appView = getLayoutInflater().inflate(
				R.layout.activity_image_bucket, null);
		appMainView.addView(appView, layoutParams);
		setTitleText("相册");
		setTitleRightButton("取消");
		helper = AlbumHelper.getHelper();
		helper.init(getApplicationContext());
		maxSize = getIntent().getIntExtra("maxSize",
				Constants.DEFAULT_IMAGE_MAX_SIZE);
	}

	/**
	 * 初始化view视图
	 */
	@Override
	protected void initComponent() {
		//绑定相册的列表
		dataList = helper.getImagesBucketList(false);
		bimap = BitmapFactory.decodeResource(getResources(),
				R.drawable.icon_addpic_unfocused);
		
		gridView = (GridView) findViewById(R.id.gridview);
		adapter = new ImageBucketAdapter(ImageAlbumActivity.this, dataList);
		gridView.setAdapter(adapter);

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				/**
				 * 通知适配器，绑定的数据发生了改变，应当刷新视图
				 */
				// adapter.notifyDataSetChanged();
				Intent intent = new Intent(ImageAlbumActivity.this,
						ImageGridActivity.class);
				intent.putExtra(ImageAlbumActivity.EXTRA_IMAGE_LIST,
						(Serializable) dataList.get(position).imageList);
				intent.putExtra("maxSize", maxSize);
				startActivity(intent);
				finish();
			}

		});
		
	}

	/**
	 * 初始化数据
	 */
	@Override
	protected void initData() {
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.top_right_btn:
			Bimp.clear();
			this.finish();
			break;

		default:
			break;
		}
	}

}
